<?php
class Admin_Users  extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('user_m');
		$this->load->model('groups_m');
	}
	
	public function index(){
		$today = date('d-m-Y');
		$this->check_permissions();
		$arr_not = array('not_username'=>array('admin'));
		$base_where = $arr_not;
		$base_where = $base_where + array('not_username'=>array('admin'));
		$base_where = $this->input->post('f_active') ? $base_where + array('active' => $this->input->post('f_active')) : $base_where;
		$base_where = $this->input->post('f_status') ? $base_where + array('status' => $this->input->post('f_status')) : $base_where;
		$base_where = $this->input->post('f_group') ? $base_where + array('group' => $this->input->post('f_group')) : $base_where;
		//status
		$s = $this->input->post('f_active');
		$s_up = 1;
		if($s == 1){//dang di lam
			$s_up = 2;//nghi viec
		}
		elseif($s == 2){
			$s_up = 1;//di lam lai
		}
		//hop dong
		
		$s = $this->input->post('f_status') ? $this->input->post('f_status') : 0;
		$s_hd = 1;//dang ky hop dong
		if($s == 1){//dang ky hop dong
			$s_hd = 0;//huy hop dong
		}
		elseif($s == 0){
			$s_hd = 1;//ky hop dong
		}
		
		//room
		$base_where = $this->input->post('f_room') ? $base_where + array('room' => $this->input->post('f_room')) : $base_where;
		
		$model = $this->user_m->get_many_by_join($base_where);
		foreach($model as $m){
			$m->s_up = $s_up;
			$m->s_hd = $s_hd;
		}
		$group_parent = $this->groups_m->group_parent($this->current_user->group_by);
		$group_parent_string = join(',',$group_parent);
		//echo $group_parent_string;exit;
		$groups = $this->groups_m->get_all(array('not_name'=>array('admin',$group_parent_string)));//
		$groups_select = array_for_select($groups,'id','description');
		//room
		$this->load->model('emp_room/room_m');
		$rooms = $this->room_m->get_all($parent_id = 0,$frefix = $this->frefix_child);//$parent_id = 0,$frefix = ''
		
		$room_select_format = array();
		array_level1($rooms,$room_select_format);
		$room_select = array_for_select($room_select_format,'id','title','frefix');
		//end:room
		if ($this->input->is_ajax_request())
		{
			$this->template->set_layout(false);
		}
		$this->action .= '+ enable|javascript:void(0)|'.lang('users:active');
		$this->template
			->set('model',$model)
			
			->set('groups_select',$groups_select)
			->set('room_select',$room_select)
			->set('s_up',$s_up)
			->set('s_hd',$s_hd)
			->set('today',$today)
			->set('action',$this->action)
			->append_js('filter.js');
			
		$this->input->is_ajax_request() ? $this->template->build($this->controller.'/partial/list') : $this->template->build($this->controller.'/'.$this->method);
	}
	
	public function create(){
		$this->check_permissions();
		$model = $this->input->post();
//		print_r($model);exit;
		$this->form_validation->set_rules('username', 'lang:users:username', 'trim|required|max_length[20]|is_unique[ser_users.username]|alpha_dash');
		$this->form_validation->set_rules('password', 'lang:users:password', 'required|max_length[20]');
		$this->form_validation->set_rules('passwordconf', 'lang:users:passwordconf', 'required|matches[password]');
		$this->form_validation->set_rules('group_id', 'lang:users:groups:group', 'required');
		
		$type =  substr($this->input->post('btnAction'), 0);
		
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$skip = array('btnAction','id','passwordconf');
				foreach($skip as $obj){
					if(array_key_exists($obj,$model))
						unset($model[$obj]); 
				}
				$id_insert = $this->user_m->insert($model);
				$this->redirect($this->method,$type,$id_insert);
			}
			else
			{
				$model['active'] = 0;
			}
		}
		
		$groups = $this->groups_m->get_all(array('not_name'=>array('admin')));//
		$groups_select = array_for_select($groups,'id','description');
		$this->view(array('model'=>$model,'groups_select'=>$groups_select));
//		$this->template
	//		->build($this->controller.'/form');
	}
	
	public function edit($id){
		$this->check_permissions();
		if($this->current_user->id != 1){
			if($id == 1)
			{
				$this->check_access();
			}
		}
		$this->action = 'save|javascript:void(0)|'.lang('global:save');
			$this->template->set('action',$this->action);
		$model = (array)$this->user_m->get($id);
		$model['password'] = "";
		$model['passwordconf'] = "";
		
		$this->kun_validate();
		$this->form_validation->set_rules('room_id', 'lang:users:room', 'required');
		
		$type =  substr($this->input->post('btnAction'), 0);
		if ( type_post($type) ) {
			//
			if ($this->form_validation->run() == TRUE)
			{
				$model = $this->input->post();
				$skip = array('btnAction','id','passwordconf');
				if(empty($model['password'])){
					unset($model['password']);
				}
				foreach($skip as $obj){
					if(array_key_exists($obj,$model))
						unset($model[$obj]); 
				}
				$this->user_m->update($id,$model);

				$this->redirect($this->method,$type,$id);
			}
			else
			{
				//$model['active'] = 0;
			}
		}
		
		$groups = $this->groups_m->get_all(array('not_name'=>array('admin')));
		$groups_select = array_for_select($groups,'id','description');
		
		
		$this->load->model('emp_room/room_m');
		$rooms = $this->room_m->get_all($parent_id = 0,$frefix = $this->frefix_child);//$parent_id = 0,$frefix = ''
		
		$room_select_format = array();
		array_level1($rooms,$room_select_format);
		$room_select = array_for_select($room_select_format,'id','title','frefix');
		
		
		$this->view(array(
					'model'=>$model,
					'room_select'=>$room_select,
					'groups_select'=>$groups_select,'msg_pass'=>lang('users:msg_pass')));
	}
	
	public function delete($id){
		if($this->current_user->id == $id)
			$this->session->set_flashdata('error', lang('users:delete:current'));
		elseif($id == 1 || $id == 2 || $id == 3)
			$this->session->set_flashdata('error', lang('users:delete:error'));
		else
			$this->user_m->delete($id);
		redirect('admin/users');
		
	}
	
	public function info(){
		$this->action = 'save|javascript:void(0)|'.lang('global:save');
			$this->template->set('action',$this->action);
		$id = $this->current_user->id;
		$model = (array)$this->user_m->get($id);
		$model['password'] = "";
		$model['passwordconf'] = "";
		
		//$this->kun_validate();
		$this->form_validation->set_rules('display_name', 'lang:users:display_name', 'required');

		
		$type =  substr($this->input->post('btnAction'), 0);
		
		if ( type_post($type) ) {
			if ($this->form_validation->run() == TRUE)
			{
				$model = $this->input->post();
				$skip = array('btnAction','id','passwordconf');
				if(empty($model['password'])){
					unset($model['password']);
				}
				foreach($skip as $obj){
					if(array_key_exists($obj,$model))
						unset($model[$obj]); 
				}
				$model['active'] = 1;
				$this->user_m->update($id,$model);

				redirect('admin/users/info');
			}
		}
		
		$groups = $this->groups_m->get_all(array('not_name'=>array('admin')));//
		$groups_select = array_for_select($groups,'id','name');

		$remove = 1;
		$this->view(array('model'=>$model,'groups_select'=>$groups_select,'msg_pass'=>lang('users:msg_pass'),'remove'=>$remove));
		//$this->edit($this->current_user->id, $view = 'info',$redirect = 'admin/users/info');
	}
	
	function kun_validate(){
		$this->form_validation->set_rules('password', 'lang:users:password', 'max_length[20]');
		$this->form_validation->set_rules('passwordconf', 'lang:users:passwordconf', 'matches[password]');
		$this->form_validation->set_rules('group_id', 'lang:users:groups:group', 'required');
		$this->form_validation->set_rules('display_name', 'lang:users:display_name', 'required|max_length[30]');
	}
	
	public function active_select(){
		$ids = $this->input->post("data");
		$this->user_m->active_select($ids);
	}
	public function active($id,$value,$date = NULL){
		
		$arr = array('active'=>$value);
		//if($value == 2){//nghi viec
		//	$date = date("d-m-Y");
		//}
		if(!empty($date)){
			$arr['day_off'] = $date;
		}
		//print_r($arr);exit;
		//print_r($date);
		$this->user_m->update_user($id,$arr);
		//print_r($arr);
		redirect('admin/users');
	}
	//ky hop dong
	public function status($id,$value,$date = NULL){
		$arr = array('status'=>$value);
		if($value == 0){//huy hop dong
			$date = date("d-m-Y");
		}
		if($date != NULL){
			$arr['date_hd'] = $date;
		}
		$this->load->model('emp_profile/profile_m');
		//print_r($arr);exit;
		$this->profile_m->update($id,$arr);
		redirect('admin/users');
	}
	
}